﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using MeetCommon.Tools;
using MVExplorer.Components;
using MeetCommon.Service;

namespace MVExplorer.Components
{
	public class MoviePlayHistory
	{
		private XmlDocument xdocHistory;
        private MeetCommon.Service.MeetServerConnector connector;
		public MoviePlayHistory()
		{
			xdocHistory = new XmlDocument();
			XmlElement elm = xdocHistory.CreateElement("history");
			xdocHistory.AppendChild(elm);
            
		}

		public void AppendSegment(XmlLinkedNode nodeSegment)
		{
			if (nodeSegment == null)
				return;
			XmlElement elmTop = xdocHistory.DocumentElement;
			XmlNode nodeImported = xdocHistory.ImportNode(nodeSegment.ParentNode, true);
			for (int i = nodeImported.ChildNodes.Count; i > 0; i--)
			{
				XmlNode node = nodeImported.ChildNodes[i - 1];
				if (node.Name == "segment" && node["id"].InnerText != nodeSegment["id"].InnerText)
					nodeImported.RemoveChild(node);
			}
			
			// V‚µ‚¢‚à‚Ì‚ªæ“ª‚É
			if (elmTop.ChildNodes.Count == 0)
				elmTop.AppendChild(nodeImported);
			else
			{
				XmlNodeList list = elmTop.SelectNodes(string.Format("//segment[child::id={0}]", nodeSegment["id"].InnerText));
				if (list != null && list.Count > 0)
					return;
				elmTop.InsertBefore(nodeImported, elmTop.ChildNodes[0]);
			}
		}

		public XmlDocument HistoryDocument
		{
			get { return xdocHistory; }
			set { xdocHistory = value; }
		}



	}
}
